﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nido.Common.BackEnd;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using DemoTest.Bll.Models;
using DemoTest.Bll.DB;
using EntityFramework.Extensions;

namespace DemoTest.Bll.Handlers
{
    public partial class StudentHandler : HandlerBase<Student, SchoolDBContext>
    {
        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public int Delete(int i)
        {
            try
            {
                int j = _context.Students.Where(x => x.StudentId > i).Delete();
                return j;
            }
            catch (Exception e)
            {
                Exception ex = e;
                return 0;
            }
        }

        /// <summary>
        /// Sample Bulk Student Uploading code
        /// </summary>
        /// <param name="studentList"></param>
        /// <returns></returns>
        public GenericResponse<List<Student>> StudentBulkInsert(List<Student> studentList)
        {
            try
            {
                _context.Configuration.AutoDetectChangesEnabled = false;
                _context.Configuration.ValidateOnSaveEnabled = false;

                this.AddBulkNew(studentList);
                if (this.SaveChanges() > 0)
                    this.LogInfo("Calling End Successfully for AddGeneric(" + studentList.ToString() + ")");
                else
                    this.LogInfo("Calling failed at save for AddGeneric(" + studentList.ToString() + ")");

                return new GenericResponse<List<Student>>(true, new string[] { "Bulk Insert operation is successfull" });
            }
            catch (Exception e)
            {
                return this.HandleException<List<Student>>(e);
            }
            finally
            {
                _context.Configuration.AutoDetectChangesEnabled = true;
                _context.Configuration.ValidateOnSaveEnabled = true;
            }
        }
    }
}
